package com.findfuture.demo.controller;

import com.findfuture.common.util.StringUtil;
import com.findfuture.demo.model.Catalog;
import com.findfuture.demo.service.ICatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 * Created by IntelliJ IDEA.
 * User: Alex
 * Date: 12-2-5
 * Time: 下午2:48
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/catalog")
@SessionAttributes("token")
public class CatalogController {

    @Autowired
    private ICatalogService catalogService;

    @RequestMapping
    public String list(ModelMap modelMap) throws Exception {
        modelMap.addAttribute("token", StringUtil.uuid());
        modelMap.addAttribute("catalogList", catalogService.getCatalogList());
        return "catalog/catalogs";
    }

    @RequestMapping("addAction")
    public String addAction(ModelMap modelMap, String name, String token, @ModelAttribute("token") String sessionToken) {
        if (token.equals(sessionToken))
            try {
                catalogService.addCatalog(name);
                modelMap.addAttribute("success", "添加成功！");
            } catch (Exception e) {
                e.printStackTrace();
                modelMap.addAttribute("error", "添加失败！");
            }
        return "forward:/catalog";
    }

    @RequestMapping("updateAction")
    public String updateAction(ModelMap modelMap, String name, int id, String token, @ModelAttribute("token") String sessionToken) {
        if (token.equals(sessionToken))
            try {
                Catalog catalog = catalogService.getCatalogById(id);
                catalog.setName(name);
                catalogService.updateCatalog(catalog);
                modelMap.addAttribute("success", "分类修改成功！");
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                modelMap.addAttribute("error", "分类修改失败！");
            }
        return "forward:/catalog";
    }
    @RequestMapping("/deleteCatalog/{catalogId}/{token}")
    public String deleteCatalog(ModelMap modelMap, @PathVariable int catalogId,
                              @PathVariable String token, @ModelAttribute("token") String sessionToken) {
        if (token.equals(sessionToken)) {
            try {
                catalogService.deleteCatalog(catalogId);
                modelMap.addAttribute("success", "操作成功！");
            } catch (Exception e) {
                e.printStackTrace();
                modelMap.addAttribute("error", "操作失败！");
            }
        }
        return "forward:/catalog";
    }
}
